package com.authorization.demo.service.impl;

import com.authorization.demo.entity.RoleTable;
import com.authorization.demo.service.CronJobService;
import com.authorization.demo.service.RoleTableService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @Remarks:
 * @ClassName: CronJobServiceImpl
 * @Author: Micora
 * @CreateTime: 2022/8/1
 */
@Service
@Slf4j
public class CronJobServiceImpl implements CronJobService {
    @Resource
    private RoleTableService roleTableService;

    @Override
    public void updateRoleTable() {
        QueryWrapper<RoleTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("STATUS", 1);
        queryWrapper.lt("UNEFFECTIVE_DATE", new Date());
        List<RoleTable> roleTableList = roleTableService.list(queryWrapper);//装载通过条件筛选得到的需要修改的值
        List<RoleTable> roleTables = new LinkedList<>(); // 用于装载返回值
        if (!roleTableList.isEmpty()) {//对需要修改的值做处理
            log.info("发现需要更新的角色信息，正在开始执行角色信息更新");
            Iterator<RoleTable> iterator = roleTableList.iterator();
            while (iterator.hasNext()) {
                RoleTable next = iterator.next();
                next.setStatus(0);
                roleTables.add(next);
            }
            roleTableService.updateBatchById(roleTables);
            log.info("角色信息更新成功");
        }
    }
}
